//
//  HWSettingView.m
//  嗨文
//
//  Created by 响当当 on 5/15/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "HWSettingView.h"

@interface HWSettingView()
@property (nonatomic, strong)UIView *bgView;
@property (nonatomic, strong)NSMutableArray *btnArr;
@property (nonatomic, assign)NSInteger textFont;
@end

@implementation HWSettingView

- (NSMutableArray *)btnArr
{
    if (!_btnArr)
    {
        _btnArr = [[NSMutableArray alloc]init];
    }
    return _btnArr;
}

- (UIView *)bgView
{
    if (!_bgView)
    {
        _bgView = [[UIView alloc]initWithFrame:self.bounds];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self addSubview:self.bgView];
        [self creatUI];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,0.5)];
        lineView.backgroundColor = JSALineColor;
        [self addSubview:lineView];
        
        self.textFont = DCDefaultTextFontSize;

    }
    return self;
}

- (void)creatUI
{
    UIButton *topTitleButton = [HWUtilties creatButtonWithFrame:CGRectMake(SCREEN_WIDTH/2-50*kUIScaleBasePlus,10*kUIScaleBasePlus,100*kUIScaleBasePlus,20*kUIScaleBasePlus)
                                                   textAligment:NSTextAlignmentCenter
                                                           text:@"折叠"
                                                       textClor:[UIColor blackColor]
                                                      buttonBGC:[UIColor clearColor]
                                                       textFont:12*kUIScaleBasePlus
                                                         target:self
                                                         method:@selector(BtnClick)];
    [topTitleButton setImage:[UIImage imageNamed:@"arrow_bottom"] forState:UIControlStateNormal];
    [topTitleButton setLeftTitleAndRightImageWithSpace:2];
    [self.bgView addSubview:topTitleButton];
    
    UIButton *textSizeMinBtn = [HWUtilties creatButtonWithFrame:CGRectMake(20*kUIScaleBasePlus,CGRectGetMaxY(topTitleButton.frame)+10*kUIScaleBasePlus,160*kUIScaleBasePlus,30*kUIScaleBasePlus)
                                                   textAligment:NSTextAlignmentCenter
                                                           text:@"A-"
                                                       textClor:[UIColor blackColor]
                                                      buttonBGC:LCHexColor(0xefefef)
                                                       textFont:18*kUIScaleBasePlus
                                                         target:self
                                                         method:@selector(textSizeBtnClick:)];
    textSizeMinBtn.tag = 0;
    [self.bgView addSubview:textSizeMinBtn];
    
    UIButton *textSizeMaxBtn = [HWUtilties creatButtonWithFrame:CGRectMake(SCREEN_WIDTH/2+7.5*kUIScaleBasePlus,CGRectGetMaxY(topTitleButton.frame)+10*kUIScaleBasePlus,160*kUIScaleBasePlus,30*kUIScaleBasePlus)
                                                   textAligment:NSTextAlignmentCenter
                                                           text:@"A+"
                                                       textClor:[UIColor blackColor]
                                                      buttonBGC:LCHexColor(0xefefef)
                                                       textFont:18*kUIScaleBasePlus
                                                         target:self
                                                         method:@selector(textSizeBtnClick:)];
    textSizeMaxBtn.tag = 1;
    [self.bgView addSubview:textSizeMaxBtn];
    
    UILabel *label = [HWUtilties creatLabelWithFrame:CGRectMake(20*kUIScaleBasePlus,CGRectGetMaxY(textSizeMinBtn.frame)+12*kUIScaleBasePlus,60*kUIScaleBasePlus,20*kUIScaleBasePlus)
                                        textAligment:NSTextAlignmentLeft
                                            textClor:[UIColor blackColor]
                                            textFont:20*kUIScaleBasePlus];
    label.text = @"颜色";
    [self.bgView addSubview:label];
    [label sizeToFit];

    CGFloat width = (SCREEN_WIDTH - CGRectGetMaxX(label.frame)-85*kUIScaleBasePlus)/4;
    
    for (int i=0;i<4;i++)
    {
        UIButton *btn = [HWUtilties creatButtonWithFrame:CGRectMake(CGRectGetMaxX(label.frame)+25*kUIScaleBasePlus+(width+15*kUIScaleBasePlus)*i,CGRectGetMaxY(textSizeMinBtn.frame)+10*kUIScaleBasePlus,width/2,width/2)
                                            textAligment:NSTextAlignmentLeft
                                                    text:@""
                                                textClor:nil
                                               buttonBGC:nil
                                                textFont:12*kUIScaleBasePlus
                                                  target:self
                                                  method:@selector(colorClick:)];
        btn.tag = i;
        switch (i)
        {
            case 0:
            {
                [btn setBackgroundColor:LCHexColor(0xf9f9f9)];
            }
                break;
            case 1:
            {
                [btn setBackgroundColor:LCHexColor(0xf3e7d0)];
            }
                break;
            case 2:
            {
                [btn setBackgroundColor:LCHexColor(0xc0dea4)];
            }
                break;
            case 3:
            {
                [btn setBackgroundColor:LCHexColor(0x1d1d1f)];
            }
                break;
            default:
                break;
        }
        
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = width/4;
        if (i == 0)
        {
            btn.layer.borderWidth = 1.5;
            btn.layer.borderColor = [UIColor redColor].CGColor;
        }
        
        [self.btnArr addObject:btn];
        [self.bgView addSubview:btn];
    }
}

- (void)BtnClick
{
    if (self.cancelSettingViewBlock)
    {
        self.cancelSettingViewBlock();
    }
}

- (void)textSizeBtnClick:(UIButton *)sender
{
    
//    if (self.textFont > 22)
//    {
//        self.textFont = self.textFont-1;
//        return;
//    }
//    else if(self.textFont < 12)
//    {
//        self.textFont = self.textFont+1;
//        return;
//    }
    
    NSInteger num = 0;
    if (self.textFontChangeBlock)
    {
        if (sender.tag == 0)
        {
            num = self.textFont-1;
        }
        else
        {
            num = self.textFont+1;
        }
        self.textFontChangeBlock(num);
        self.textFont = num;
    }
}

- (void)colorClick:(UIButton *)sender
{
    
    if (self.colorChangeBlock)
    {
        self.colorChangeBlock([NSString stringWithFormat:@"%ld",sender.tag]);
    }
    
    sender.layer.borderWidth = 1.5;
    sender.layer.borderColor = [UIColor redColor].CGColor;
    
    for (int i=0; i<self.btnArr.count; i++)
    {
        UIButton *btn = self.btnArr[i];
        if (i!=sender.tag)
        {
            btn.layer.borderWidth = 0.1;
            btn.layer.borderColor = JSALineColor.CGColor;
        }
    }
}

@end
